{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ce8108d7",
   "metadata": {},
   "outputs": [],
   "source": [
    "pip install pyquery"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ba7a063b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyquery import PyQuery as pq\n",
    "import requests\n",
    "\n",
    "resp = requests.get('http://www.baidu.com')\n",
    "resp.encoding = 'utf-8'\n",
    "\n",
    "doc = pq(resp.text)\n",
    "print(doc('a'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2cab008d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyquery import PyQuery as pq\n",
    "\n",
    "doc = pq(url=\"http://www.baidu.com\")\n",
    "print(doc('head'))\n",
    "\n",
    "# doc = pq(filename=\"./somePath.html\")\n",
    "# print(doc('head'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5e787106",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyquery import PyQuery as pq\n",
    "import requests\n",
    "\n",
    "resp = requests.get('http://www.baidu.com')\n",
    "resp.encoding = 'utf-8'\n",
    "\n",
    "doc = pq(resp.text)\n",
    "print(doc('.s_ipt').attr('name'))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b6f9bafc",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyquery import PyQuery as pq\n",
    "import requests\n",
    "\n",
    "resp = requests.get('http://www.baidu.com')\n",
    "resp.encoding = 'utf-8'\n",
    "\n",
    "doc = pq(resp.text)\n",
    "print(doc('#form').find(\".s_ipt_wr\").attr('class'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b239e276",
   "metadata": {},
   "source": [
    "- PqObject.find(selectStr)\n",
    "- PqObject.children(selectStr)\n",
    "- PqObject.parent()\n",
    "- PqObject.parents(selectStr)\n",
    "- PqObject.slibings(selectStr) 兄弟元素\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "28f87f71",
   "metadata": {},
   "source": [
    "## 遍历\n",
    "\n",
    "- .items() 然后通过 for in 便利 items()返回的数组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1174f213",
   "metadata": {},
   "source": [
    "## 获取属性\n",
    "\n",
    "- .attr(attrName)\n",
    "- .attr.attrName"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2abcbf1d",
   "metadata": {},
   "source": [
    "## 获取文本\n",
    "\n",
    "- PqObject.text()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c794f56",
   "metadata": {},
   "source": [
    "## 获取html内容\n",
    "\n",
    "- PqObject.html()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "baf8374f",
   "metadata": {},
   "source": [
    "## DOM 操作\n",
    "\n",
    "- PqObject.addClass\n",
    "- PqObject.removeClass\n",
    "- PqObject.attr(name, value)\n",
    "- PqObject.css('font-size', '14px')\n",
    "- PqObjectSelf.remove()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b17c38a6",
   "metadata": {},
   "source": [
    "## CSS伪类选择器\n",
    "\n",
    "- doc('li:first-child')\n",
    "- doc('li:last-child')\n",
    "- doc('li:nth-child(2)')\n",
    "- doc('li:gt(2)')\n",
    "- doc('li:nth-child(2n)')\n",
    "- doc('li:nth-child(odd)')\n",
    "- doc('li:contains(second)')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1822c3c2",
   "metadata": {},
   "source": [
    "## 更多内容查看官方文档\n",
    "\n",
    "https://pyquery.readthedocs.io/en/latest/#quickstart\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
